<template>
  <div class="search-picture-page">
    <!-- 页面头部 -->
    <div class="page-header">
      <h1 class="page-title">以图搜图</h1>
      <p class="page-description">通过相似图片发现更多精彩内容</p>
    </div>

    <a-row :gutter="[24, 24]">
      <!-- 左侧：原图信息 -->
      <a-col :xs="24" :md="8" :lg="6">
        <a-card title="原图信息" class="original-card">
          <div class="original-image-container">
        <img
          :alt="picture.name"
          :src="picture.thumbnailUrl ?? picture.url"
              class="original-image"
            />
          </div>
          <div class="original-info">
            <h4 class="original-name">{{ picture.name || '未知图片' }}</h4>
            <div class="original-meta">
              <div class="meta-item">
                <span class="meta-label">分类：</span>
                <span class="meta-value">{{ picture.category || '默认' }}</span>
              </div>
              <div class="meta-item">
                <span class="meta-label">大小：</span>
                <span class="meta-value">{{ formatFileSize(picture.picSize || 0) }}</span>
              </div>
              <div class="meta-item">
                <span class="meta-label">尺寸：</span>
                <span class="meta-value">{{ picture.picWidth }}x{{ picture.picHeight }}</span>
              </div>
            </div>
          </div>
        </a-card>
      </a-col>

      <!-- 右侧：搜索结果 -->
      <a-col :xs="24" :md="16" :lg="18">
        <a-card class="results-card">
          <template #title>
            <div class="results-header">
              <span>相似图片</span>
              <span v-if="!loading" class="result-count">找到 {{ dataList.length }} 张相似图片</span>
            </div>
      </template>

          <!-- 加载状态 -->
          <div v-if="loading" class="loading-container">
            <div class="search-loading-animation">
              <!-- 搜索动画中心图标 -->
              <div class="search-center">
                <div class="search-icon">
                  <svg viewBox="0 0 24 24" fill="currentColor">
                    <path d="M15.5 14h-.79l-.28-.27C15.41 12.59 16 11.11 16 9.5 16 5.91 13.09 3 9.5 3S3 5.91 3 9.5 5.91 16 9.5 16c1.61 0 3.09-.59 4.23-1.57l.27.28v.79l5 4.99L20.49 19l-4.99-5zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"/>
                  </svg>
                </div>
              </div>
              
              <!-- 扫描线效果 -->
              <div class="scan-lines">
                <div class="scan-line scan-line-1"></div>
                <div class="scan-line scan-line-2"></div>
                <div class="scan-line scan-line-3"></div>
              </div>
              
              <!-- 旋转的环形进度 -->
              <div class="progress-rings">
                <div class="progress-ring ring-1"></div>
                <div class="progress-ring ring-2"></div>
                <div class="progress-ring ring-3"></div>
              </div>
              
              <!-- 浮动的小图片图标 -->
              <div class="floating-icons">
                <div class="floating-icon icon-1">📷</div>
                <div class="floating-icon icon-2">🖼️</div>
                <div class="floating-icon icon-3">🎨</div>
                <div class="floating-icon icon-4">📸</div>
                <div class="floating-icon icon-5">🖥️</div>
                <div class="floating-icon icon-6">💫</div>
              </div>
              
              <!-- 文字提示 -->
              <div class="loading-text">
                <h3 class="loading-title">正在搜索相似图片</h3>
                <p class="loading-subtitle">AI正在为您匹配最相似的图片内容...</p>
                <div class="loading-dots">
                  <span class="dot dot-1">●</span>
                  <span class="dot dot-2">●</span>
                  <span class="dot dot-3">●</span>
                </div>
              </div>
            </div>
          </div>

          <!-- 搜索结果 -->
    <a-list
            v-else-if="dataList.length > 0"
            :grid="{ gutter: 16, xs: 1, sm: 2, md: 2, lg: 3, xl: 4, xxl: 5 }"
      :data-source="dataList"
          >
            <template #renderItem="{ item }">
              <a-list-item>
                <div class="result-item" @click="openImage(item)">
                  <div class="result-image-container">
                    <img
                      :src="item.thumbUrl"
                      :alt="item.title || '相似图片'"
                      class="result-image"
                    />
                    <div class="result-overlay">
                      <div class="result-actions">
                        <a-button type="primary" shape="circle" @click.stop="openImage(item)">
                          <template #icon>
                            <svg viewBox="0 0 24 24" fill="currentColor">
                              <path d="M14,3V5H17.59L7.76,14.83L9.17,16.24L19,6.41V10H21V3M19,19H5V5H12V3H5C3.89,3 3,3.9 3,5V19A2,2 0 0,0 5,21H19A2,2 0 0,0 21,19V12H19V19Z"/>
                            </svg>
              </template>
                        </a-button>
                      </div>
                    </div>
                  </div>
                  <div class="result-info">
                    <p class="result-title">{{ item.title || '未知标题' }}</p>
                    <p class="result-source">{{ getDomainFromUrl(item.fromUrl) }}</p>
                  </div>
                </div>
        </a-list-item>
      </template>
    </a-list>

          <!-- 空状态 -->
          <a-empty v-else description="未找到相似图片">
            <template #image>
              <div class="empty-icon">🖼️</div>
            </template>
            <p>没有找到与此图片相似的内容，请尝试其他图片</p>
          </a-empty>
        </a-card>
      </a-col>
    </a-row>
  </div>
</template>

<script setup lang="ts">
import { computed, onMounted, ref } from 'vue'
import {
  getPictureVoByIdUsingGet,
  searchPictureByPictureUsingPost,
} from '@/api/pictureController.ts'
import { message } from 'ant-design-vue'
import { useRoute } from 'vue-router'

const route = useRoute()

const pictureId = computed(() => {
  return route.query?.pictureId
})
const picture = ref<API.PictureVO>({})

// 获取图片详情
const fetchPictureDetail = async () => {
  try {
    const res = await getPictureVoByIdUsingGet({
      id: pictureId.value,
    })
    if (res.data.code === 0 && res.data.data) {
      picture.value = res.data.data
    } else {
      message.error('获取图片详情失败，' + res.data.message)
    }
  } catch (e: any) {
    message.error('获取图片详情失败：' + e.message)
  }
}

onMounted(() => {
  fetchPictureDetail()
})

// 以图搜图结果
const dataList = ref<API.SoImageSearchResult[]>([])
const loading = ref<boolean>(true)

// 获取搜图结果
const fetchResultData = async () => {
  loading.value = true;
  try {
    const res = await searchPictureByPictureUsingPost({
      pictureId: pictureId.value,
    })
    if (res.data.code === 0 && res.data.data) {
      dataList.value = res.data.data ?? []
    } else {
      message.error('获取数据失败，' + res.data.message)
    }
  } catch (e: any) {
    message.error('获取数据失败，' + e.message)
  }
  loading.value = false;
}

// 打开图片链接
const openImage = (item: API.SoImageSearchResult) => {
  if (item.fromUrl) {
    window.open(item.fromUrl, '_blank')
  }
}

// 格式化文件大小
const formatFileSize = (size: number): string => {
  if (size === 0) return '0 B'
  const k = 1024
  const sizes = ['B', 'KB', 'MB', 'GB']
  const i = Math.floor(Math.log(size) / Math.log(k))
  return parseFloat((size / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]
}

// 从URL中提取域名
const getDomainFromUrl = (url: string): string => {
  try {
    return new URL(url).hostname
  } catch {
    return '未知来源'
  }
}

// 页面加载时请求数据
onMounted(async () => {
  await fetchPictureDetail()
  await fetchResultData()
})
</script>

<style scoped>
.search-picture-page {
  padding: 24px;
  background: #f5f5f5;
  min-height: calc(100vh - 120px);
}

/* 页面头部 */
.page-header {
  text-align: center;
  margin-bottom: 32px;
  background: white;
  padding: 32px;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
}

.page-title {
  font-size: 2rem;
  font-weight: 600;
  color: #1890ff;
  margin: 0 0 8px 0;
}

.page-description {
  color: #666;
  margin: 0;
  font-size: 1rem;
}

/* 原图卡片 */
.original-card {
  height: 100%;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.08);
}

.original-image-container {
  width: 100%;
  height: 200px;
  border-radius: 8px;
  overflow: hidden;
  margin-bottom: 16px;
}

.original-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.original-info {
  padding: 16px 0;
}

.original-name {
  font-size: 1.1rem;
  font-weight: 600;
  color: #333;
  margin: 0 0 16px 0;
}

.original-meta {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.meta-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.meta-label {
  color: #666;
  font-weight: 500;
}

.meta-value {
  color: #333;
  font-weight: 400;
}

/* 结果卡片 */
.results-card {
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.08);
}

.results-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.result-count {
  color: #666;
  font-size: 0.9rem;
  font-weight: normal;
}

/* 加载状态 */
.loading-container {
  text-align: center;
  padding: 120px 20px;
  position: relative;
  background: linear-gradient(135deg, #f8fafc 0%, #e2e8f0 100%);
  border-radius: 16px;
  overflow: hidden;
}

.search-loading-animation {
  position: relative;
  width: 320px;
  height: 320px;
  margin: 0 auto;
}

/* 中心搜索图标 */
.search-center {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  z-index: 10;
}

.search-icon {
  width: 80px;
  height: 80px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  animation: pulse 2s ease-in-out infinite;
  box-shadow: 0 8px 32px rgba(102, 126, 234, 0.3);
}

.search-icon svg {
  width: 32px;
  height: 32px;
}

@keyframes pulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.1); }
}

/* 扫描线效果 */
.scan-lines {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

.scan-line {
  position: absolute;
  width: 100%;
  height: 2px;
  background: linear-gradient(90deg, transparent 0%, #667eea 50%, transparent 100%);
  animation: scan 3s ease-in-out infinite;
}

.scan-line-1 {
  top: 30%;
  animation-delay: 0s;
}

.scan-line-2 {
  top: 50%;
  animation-delay: 1s;
}

.scan-line-3 {
  top: 70%;
  animation-delay: 2s;
}

@keyframes scan {
  0%, 100% { opacity: 0; transform: translateX(-100%); }
  50% { opacity: 1; transform: translateX(100%); }
}

/* 旋转环形进度 */
.progress-rings {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.progress-ring {
  position: absolute;
  border: 2px solid transparent;
  border-radius: 50%;
  animation: rotate 4s linear infinite;
}

.ring-1 {
  width: 120px;
  height: 120px;
  top: -60px;
  left: -60px;
  border-top-color: #667eea;
  border-right-color: #667eea;
  animation-duration: 3s;
}

.ring-2 {
  width: 160px;
  height: 160px;
  top: -80px;
  left: -80px;
  border-bottom-color: #764ba2;
  border-left-color: #764ba2;
  animation-duration: 4s;
  animation-direction: reverse;
}

.ring-3 {
  width: 200px;
  height: 200px;
  top: -100px;
  left: -100px;
  border-top-color: #4facfe;
  border-bottom-color: #4facfe;
  animation-duration: 5s;
}

@keyframes rotate {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

/* 浮动图标 */
.floating-icons {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

.floating-icon {
  position: absolute;
  font-size: 1.5rem;
  animation: float 6s ease-in-out infinite;
  opacity: 0.7;
}

.icon-1 {
  top: 10%;
  left: 20%;
  animation-delay: 0s;
}

.icon-2 {
  top: 20%;
  right: 15%;
  animation-delay: 1s;
}

.icon-3 {
  bottom: 20%;
  left: 15%;
  animation-delay: 2s;
}

.icon-4 {
  bottom: 15%;
  right: 20%;
  animation-delay: 3s;
}

.icon-5 {
  top: 50%;
  left: 5%;
  animation-delay: 4s;
}

.icon-6 {
  top: 45%;
  right: 5%;
  animation-delay: 5s;
}

@keyframes float {
  0%, 100% {
    transform: translateY(0) rotate(0deg);
    opacity: 0.7;
  }
  25% {
    transform: translateY(-20px) rotate(90deg);
    opacity: 1;
  }
  50% {
    transform: translateY(-10px) rotate(180deg);
    opacity: 0.8;
  }
  75% {
    transform: translateY(-30px) rotate(270deg);
    opacity: 0.9;
  }
}

/* 加载文字 */
.loading-text {
  position: absolute;
  bottom: -80px;
  left: 50%;
  transform: translateX(-50%);
  text-align: center;
  width: 100%;
}

.loading-title {
  font-size: 1.4rem;
  font-weight: 600;
  color: #334155;
  margin: 0 0 8px 0;
  animation: fadeInUp 1s ease-out;
}

.loading-subtitle {
  font-size: 0.9rem;
  color: #64748b;
  margin: 0 0 16px 0;
  animation: fadeInUp 1s ease-out 0.3s both;
}

.loading-dots {
  display: flex;
  justify-content: center;
  gap: 8px;
  animation: fadeInUp 1s ease-out 0.6s both;
}

.dot {
  font-size: 0.8rem;
  color: #667eea;
  animation: bounce 1.5s ease-in-out infinite;
}

.dot-1 { animation-delay: 0s; }
.dot-2 { animation-delay: 0.3s; }
.dot-3 { animation-delay: 0.6s; }

@keyframes bounce {
  0%, 80%, 100% {
    transform: scale(1);
    opacity: 0.7;
  }
  40% {
    transform: scale(1.3);
    opacity: 1;
  }
}

@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* 结果项目 */
.result-item {
  background: white;
  border-radius: 8px;
  overflow: hidden;
  transition: all 0.3s ease;
  cursor: pointer;
  border: 1px solid #f0f0f0;
}

.result-item:hover {
  transform: translateY(-4px);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
  border-color: #1890ff;
}

.result-image-container {
  position: relative;
  width: 100%;
  height: 160px;
  overflow: hidden;
}

.result-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.3s ease;
}

.result-item:hover .result-image {
  transform: scale(1.05);
}

.result-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transition: opacity 0.3s ease;
}

.result-item:hover .result-overlay {
  opacity: 1;
}

.result-actions {
  display: flex;
  gap: 8px;
}

.result-info {
  padding: 12px;
}

.result-title {
  font-size: 0.9rem;
  font-weight: 500;
  color: #333;
  margin: 0 0 4px 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.result-source {
  font-size: 0.8rem;
  color: #666;
  margin: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* 空状态 */
.empty-icon {
  font-size: 3rem;
  margin-bottom: 16px;
}

/* 响应式 */
@media (max-width: 768px) {
  .search-picture-page {
    padding: 16px;
  }
  
  .page-header {
    padding: 24px 16px;
    margin-bottom: 24px;
  }
  
  .page-title {
    font-size: 1.5rem;
  }
  
  .original-image-container {
    height: 150px;
  }
  
  .result-image-container {
    height: 120px;
  }
}
</style>
